#include "main.h"


TPlane::TPlane(TPoint p1,TPoint p2,TPoint p3)
{
	_p1=p1;
	_p2=p2;
	_p3=p3;
}


TPlane::~TPlane(void)
{
}

float TPlane::distanceToPoint(TPoint pnt)
{
	//ax+bx+cx+d=0 as the plane formula
	// Assume d is always equal to det of (x1,x2,x3,
	//									   y1,y2,y2,
	//									   z1,z2,z3) matrix
	
	float detPlane;
	float a,b,c,d;
	float distance;

	detPlane=Tmatrix(_p1.X(),_p1.Y(),_p1.Z(),
					 _p2.X(),_p2.Y(),_p2.Z(),
					 _p3.X(),_p3.Y(),_p3.Z()).det();

	d=detPlane;

	a=0-Tmatrix(1,_p1.Y(),_p1.Z(),
				1,_p2.Y(),_p2.Z(),
				1,_p3.Y(),_p3.Z()).det();
	
	b=0-Tmatrix(_p1.X(),1,_p1.Z(),
		    	_p2.X(),1,_p2.Z(),
				_p3.X(),1,_p3.Z()).det();

	c=0-Tmatrix(_p1.X(),_p1.Y(),1,
		    	_p2.X(),_p2.Y(),1,
				_p3.X(),_p3.Y(),1).det();

	//Distance to plane formula is abs(a*x1+b*y1+c*z1+d)/sqrt(a*a+b*b+c*c)
	distance=abs(a*pnt.X()+b*pnt.Y()+c*pnt.Z()+d)/sqrt(pow(a,2)+pow(b,2)+pow(c,2));

	return distance;
}
